package com.example.win.android_banduyujia.peopleactivity.activity.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;
import com.example.win.android_banduyujia.ui.popupWindow.Popupwindows;

import java.io.File;

/**
 * Created by huchun on 2015/8/20.
 */
public class RealActivity extends BaseTitleBarActivity implements View.OnClickListener {
    public static final String  TAG = RealActivity.class.getSimpleName();

    private  ImageView  mImageCard, mImageHonor;
    private  TextView   mTxtHonorphone, mTxtCername, mTxtCard,mTxtPassport;
    private  Button     mBtnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_name_book);

        InitTitles();
        InitViews();
    }

    private void InitTitles() {
        setTitle(R.string.text_view_real_name_name);
        showLeftBackButton();
    }

    private void InitViews() {
        /*mImageCard = (ImageView) findViewById(R.id.layout_text_view_private_homes);*/
        mImageHonor = (ImageView) findViewById(R.id.layout_honor_image_view);

        mTxtHonorphone = (TextView) findViewById(R.id.honor_title_list_item_name);
        mTxtCername = (TextView) findViewById(R.id.honor_title_list_item_name);

        mTxtCard = (TextView)findViewById(R.id.text_view_real_card);
        mTxtPassport = (TextView)findViewById(R.id.text_view_real_passport);
        //mTxtAgement = (TextView) findViewById(R.id.layout_linear_text_view_agreement);// 协议

        mBtnSubmit = (Button) findViewById(R.id.layout_button_data_add);

        mBtnSubmit.setOnClickListener(this);

        mTxtCername.setOnClickListener(this);
        mTxtHonorphone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 new Popupwindows(RealActivity.this, mImageHonor);
            }
        });
     }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_honor_image_view: // 荣誉照片
                 honorActivity();
                break;
            case R.id.honor_title_list_item_name: // 上传文本荣誉证
                //honorActivity();
                break;
            case R.id.text_view_real_card: // 身份证

                break;
            case R.id.text_view_real_passport: // 护照

                break;
            case R.id.layout_button_data_add: // 提交

                break;
        }
    }

    private void honorActivity() {
        /*Popupwindows popupwindows = new Popupwindows(RealActivity.this);
        popupwindows.showAtLocation(mImageHonor);*/
    }

    public class Popupwindows extends PopupWindow{
        public Popupwindows(Context context, View parent) {
            View view = View.inflate(context, R.layout.title_pic_item, null);
            view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_ins));
            LinearLayout ll_pop = (LinearLayout)view.findViewById(R.id.ll_popup);
            ll_pop.startAnimation(AnimationUtils.loadAnimation(context, R.anim.push_bottom_in_2));

            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();

            Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
            Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
            Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);
            bt1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    photo();
                    dismiss();
                }
            });
            bt2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    picture();
                    dismiss();
                }
            });
            bt3.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }

    private static final int TAKE_PICTURE = 0x000000;
    private static final int TAKE_PHOTO = 0x000001;
    private static final int TAKE_CROP = 0x000002;

    private void photo() {
        String  status = Environment.getExternalStorageState();
        if(status.equals(Environment.MEDIA_MOUNTED)){
            File dir =new File(Environment.getExternalStorageDirectory() + "/image/");
            if(!dir.exists())
                dir.mkdirs();

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // 下面这句指定调用相机拍照后的照片存储的路径
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), ".jpg")));
            intent.putExtra("return-data", true);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
            startActivityForResult(intent, TAKE_PICTURE);
        }else{
            showToast("没有卡");
        }
    }

    private void picture() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, TAKE_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case TAKE_PHOTO: // 如果是直接从相册获取
                startPhotoZoom(data.getData());
                break;
            case TAKE_PICTURE: // 如果是调用相机拍照时
                File temp = new File(Environment.getExternalStorageDirectory()+"/.jpg");
                startPhotoZoom(Uri.fromFile(temp));
                break;
            case TAKE_CROP:  // 取得裁剪后的图片
                if(data != null){
                    setPicToView(data);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    // 裁剪图片方法实现 173 * 174
    private void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");

        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectX", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, TAKE_CROP);
    }

    //	保存裁剪之后的图片数据 201 * 202
    private void setPicToView(Intent data) {
        Bundle  bundle = data.getExtras();
        if(bundle != null){
            Bitmap photo = bundle.getParcelable("data");
            Drawable drawable = new BitmapDrawable(photo);

            /* 下面注释的方法是将裁剪之后的图片以Base64Coder的字符方式上 传到服务器，
		       QQ头像上传采用的方法跟这个类似 213*/
            mImageHonor.setImageBitmap(photo);
        }
    }
}

